package com.purong.purong.activity;

import android.annotation.TargetApi;
import android.os.Build;
import android.support.annotation.NonNull;
import android.util.Log;
import com.dreamliner.easypermissions.AfterPermissionGranted;
import com.dreamliner.easypermissions.EasyPermissions;
import com.purong.purong.PermissionConstant;
import com.purong.purong.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public abstract class BasePermActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {

    private boolean isNeverAskAgain = false;
    @Override
    public void onPermissionsGranted(int i, List<String> list) {
        if (list.size() == getPermissions().length) {
            doPermissinosSuc();
        } else {
            Log.e("BaseAct", "onPermissionsGranted: 有部分权限没有允许");
        }
    }

    @Override
    public void onPermissionsDenied(int i, List<String> list) {
        isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(list) + "没有允许，请打开“设置”允许所有权限。",
                R.string.setting, android.R.string.cancel, list);
    }

    //	@Override
//	protected void onResume() {
//		super.onResume();
//
//	}
    protected abstract String[] getPermissions();

    //运行时权限管理
    protected abstract void doPermissinosSuc();

    public final static int RC_ALL_PERM = 0x100;
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @AfterPermissionGranted(RC_ALL_PERM)
    protected void checkPermission(boolean showDialog) {
        String[] perms = getPermissions();

        if (EasyPermissions.hasPermissions(this, perms)) {
            doPermissinosSuc();
        } else {
            if (!showDialog)
                return;
            String[] denieds = EasyPermissions.getDeniedPermissions(this, perms);
            if (!isNeverAskAgain) {
                EasyPermissions.requestPermissions(this, this.getResources().getString(R.string.app_name)+"需要申请" + PermissionConstant.getPermsName(denieds) + "，请点击允许。", RC_ALL_PERM, denieds);
            } else {

                ArrayList<String> deniedList = new ArrayList<>();
                Collections.addAll(deniedList, denieds);
                isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(denieds) + "没有允许，请打开“设置”允许所有权限。",
                        R.string.setting, android.R.string.cancel, deniedList);
            }
        }
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @AfterPermissionGranted(RC_ALL_PERM)
    protected void checkPermission() {

        String[] perms = getPermissions();

        if (EasyPermissions.hasPermissions(this, perms)) {
            doPermissinosSuc();
        } else {
            String[] denieds = EasyPermissions.getDeniedPermissions(this, perms);
            if (!isNeverAskAgain) {
                EasyPermissions.requestPermissions(this, "普融需要申请" + PermissionConstant.getPermsName(denieds) + "，请点击允许。", RC_ALL_PERM, denieds);
            } else {

                ArrayList<String> deniedList = new ArrayList<>();
                Collections.addAll(deniedList, denieds);
                isNeverAskAgain = EasyPermissions.checkDeniedPermissionsNeverAskAgain(this, PermissionConstant.getPermsName(denieds) + "没有允许，请打开“设置”允许所有权限。",
                        R.string.setting, android.R.string.cancel, deniedList);
            }
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }
    @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
    public boolean hasDestroy(){
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1)
            return false;
        if (isDestroyed()){
            return true;
        }else {
            return false;
        }
    }
}
